package com.rabbiter.ordering.mapper;
import com.rabbiter.ordering.entity.Package;
import com.rabbiter.ordering.entity.PackageFood;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PackageMapper {
    @Select("SELECT * FROM package")
    List<Package> selectAll();

    @Select("SELECT * FROM package WHERE id = #{id}")
    Package selectById(Integer id);

    @Select("SELECT pf.*, f.name, f.img, f.price " +
            "FROM package_food pf " +
            "LEFT JOIN foods f ON pf.food_id = f.id " +
            "WHERE pf.package_id = #{packageId}")
    List<PackageFood> selectFoodsByPackageId(Integer packageId);

    @Insert("INSERT INTO package(name, img, price, descr) " +
            "VALUES(#{name}, #{img}, #{price}, #{descr})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Package pkg);

    @Update("UPDATE package SET name=#{name}, img=#{img}, price=#{price}, descr=#{descr} " +
            "WHERE id = #{id}")
    int update(Package pkg);

    @Delete("DELETE FROM package WHERE id = #{id}")
    int delete(Integer id);

    @Insert("INSERT INTO package_food(package_id, food_id, num) VALUES(#{packageId}, #{foodId}, #{num})")
    int insertPackageFood(PackageFood packageFood);

    @Delete("DELETE FROM package_food WHERE package_id = #{packageId}")
    int deletePackageFoods(Integer packageId);
}
